#Parser for perfect world ski. Games include
#-Forsaken World
#-Jade Dynasty
#-Perfect World

import os, sys
from Sanae3D.lib.ModelObject import Model3D
from Sanae3D.lib.StructReader import StructReader

class forsakenWorld_ski(Model3D):
    '''Replace 'forsakenWorld_ski' with a suitable name of your choice. Try to follow
    the standard: GameName_FormatExt'''
    
    def __init__(self, obj3D=None, outFile=None, inFile=None):
        
        super(forsakenWorld_ski,self).__init__("XAC")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        
    def parse_file(self):
        '''Main parser method. Can be replaced'''
        
        magic = self.inFile.read_string(8)
        meshType = self.inFile.read_long()
        unk1, numMesh, vertType, unk2  = self.inFile.read_long(4)
        numTex = self.inFile.read_long()
        numMat = self.inFile.read_long()
        numBones = self.inFile.read_long()
        unk3 = self.inFile.read_long()
        typeMask = self.inFile.read_long()
        print magic, meshType, vertType, numMesh, numTex, numMat, numBones
        #60 zeroes
        self.inFile.read_long(15)
        
        if meshType == 9:
            #self.parse_bones()
            pass
        for i in range(numTex):
            strLen = self.inFile.read_long()
            texName = self.inFile.read_string(strLen)
        
        for i in range(numMat):
            matHeader = self.inFile.read_string(11)
            self.inFile.read_float(16)
            self.inFile.read_float()
            self.inFile.read_byte()
            
        for i in range(numMesh):
            strLen = self.inFile.read_long()
            meshName = self.inFile.read_string(strLen)
            texNum, matNum = self.inFile.read_long(2)
            
            self.create_object(meshName)
            
            
            if vertType == 0:
                self.inFile.read_byte(4)
            numVerts = self.inFile.read_long()
            numFaces = self.inFile.read_long() / 3
            
            print "%d vertices, %d faces" %(numVerts, numFaces)
            
            for j in range(numVerts):
                vx, vy, vz = self.inFile.read_float(3)
                if vertType == 1:
                    w1, w2, w3 = self.inFile.read_float(3)
                    b1, b2, b3, b4 = self.inFile.read_byte(4)
                nx, ny, nz = self.inFile.read_float(3)
                tu, tv = self.inFile.read_float(2)
                
                self.create_vertex(meshName, j)
                self.add_coords(meshName, j, [vx, vy, vz])
                
            for j in range(numFaces):
                v1, v2, v3 = self.inFile.read_short(3)
                
                self.create_face(meshName, j)
                self.add_face_verts(meshName, j, [v1, v2, v3])
            
def read_file(path):
    '''Read the file'''
    
    openFile = StructReader(open(path, 'rb'))
    obj = forsakenWorld_ski(inFile=openFile)
    obj.parse_file()
    openFile.close()
    return obj

def write_file(path):
    
    pass

def definitions():
    '''Return the header, extension, and a description of the format'''
    
    return "MOXBIKSA", "SKI", "Perfect world ski format"

if __name__ == '__main__':
    
    file1 = "test.ski"
    read_file(file1)